package university.dropcourse.service;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import university.dropcourse.conversion.DocenteConv;
import university.dropcourse.conversion.StudenteConv;
import university.dropcourse.dto.DocenteDto;
import university.dropcourse.dto.StudenteDto;
import university.dropcourse.entity.Docente;
import university.dropcourse.entity.Studente;

/**
 * Session Bean implementation class LoginManager
 */
@Stateless
@LocalBean
@WebService
public class LoginManagerBean {

	@PersistenceContext
	private EntityManager manager;	
	@EJB
    DocenteConv docConv;
	@EJB
	StudenteConv studConv;
	
    public LoginManagerBean() {  }
    
    public DocenteDto verificaDocente(String matricola, String password){
    	
    	Query q = manager.createQuery("SELECT d FROM Docente as d WHERE d.matricola= ?1 AND d.password = ?2");
    	q.setParameter(1, matricola);
    	q.setParameter(2, password);
    	@SuppressWarnings("unchecked")
		List<Docente> result = (List<Docente>) q.getResultList();
    	if(result.isEmpty())
    		return null;
    	else
    		return docConv.fromEntity(result.get(0));
    	
    }
    
  public StudenteDto verificaStudente(String matricola, String password){
    	
    	Query q = manager.createQuery("SELECT s FROM Studente as s WHERE s.matricola= ?1 AND s.password = ?2");
    	q.setParameter(1, matricola);
    	q.setParameter(2, password);
    	@SuppressWarnings("unchecked")
		List<Studente> result = (List<Studente>) q.getResultList();
    	if(result.isEmpty())
    		return null;
    	else
    		return studConv.fromEntity(result.get(0));
    	
    }

}
